<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 Sorted Set：字典与跳跃表的完美结合</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-shadow {
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .card-shadow:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            padding: 16px;
            font-family: 'Consolas', 'Monaco', monospace;
            overflow-x: auto;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
        
        .hover-scale {
            transition: transform 0.3s ease;
        }
        
        .hover-scale:hover {
            transform: scale(1.05);
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center animate-fadeInUp">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                深入理解 Sorted Set
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                字典与跳跃表的完美结合
            </p>
            <div class="flex justify-center space-x-4 text-lg">
                <span class="flex items-center">
                    <i class="fas fa-layer-group mr-2"></i>
                    数据结构设计
                </span>
                <span class="flex items-center">
                    <i class="fas fa-bolt mr-2"></i>
                    高性能实现
                </span>
                <span class="flex items-center">
                    <i class="fas fa-database mr-2"></i>
                    Redis 核心技术
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl card-shadow p-8 mb-12 animate-fadeInUp">
            <p class="text-lg leading-relaxed text-gray-700 indent-8">
                在数据结构设计中，<span class="font-bold text-purple-600">Sorted Set（有序集合）</span>是一个复杂但非常高效的数据结构，常见于如Redis等系统中。它通常结合使用字典和跳跃表（Skip List）来实现高效的元素存储和操作。
            </p>
        </div>

        <!-- Section 1: 数据结构概述 -->
        <section class="mb-12 animate-fadeInUp" style="animation-delay: 0.2s">
            <h2 class="text-3xl font-bold mb-6 section-title">
                <i class="fas fa-cube mr-3 text-purple-600"></i>数据结构概述
            </h2>
            <div class="bg-white rounded-xl card-shadow p-8">
                <p class="text-lg leading-relaxed text-gray-700 indent-8">
                    <span class="font-bold text-purple-600">Sorted Set</span> 是一个集合，其中的每个元素都有一个关联的分数（score）。元素会根据其分数进行排序，支持快速的元素插入、删除和查找操作。
                </p>
                
                <!-- Visual Representation -->
                <div class="mt-8 bg-gray-50 rounded-lg p-6">
                    <div class="mermaid">
                        graph LR
                            A[元素1<br/>分数: 1.5] --> B[元素2<br/>分数: 2.3]
                            B --> C[元素3<br/>分数: 3.7]
                            C --> D[元素4<br/>分数: 5.2]
                            style A fill:#e0e7ff,stroke:#667eea
                            style B fill:#e0e7ff,stroke:#667eea
                            style C fill:#e0e7ff,stroke:#667eea
                            style D fill:#e0e7ff,stroke:#667eea
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 2: 字典和跳跃表的角色 -->
        <section class="mb-12 animate-fadeInUp" style="animation-delay: 0.3s">
            <h2 class="text-3xl font-bold mb-6 section-title">
                <i class="fas fa-cogs mr-3 text-purple-600"></i>字典和跳跃表的角色
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <!-- Skip List Card -->
                <div class="feature-card rounded-xl p-6 hover-scale">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-purple-100 rounded-lg flex items-center justify-center mr-4">
                            <i class="fas fa-sort-amount-up text-purple-600 text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold">跳跃表（Skip List）</h3>
                    </div>
                    <p class="text-gray-700 leading-relaxed">
                        是一种基于链表的概率型数据结构，用于实现有序的数据访问。它通过多级索引实现了对元素的快速访问和排序操作。
                    </p>
                    <div class="mt-4 flex items-center text-sm text-purple-600">
                        <i class="fas fa-check-circle mr-2"></i>
                        <span>O(log n) 时间复杂度</span>
                    </div>
                </div>

                <!-- Dictionary Card -->
                <div class="feature-card rounded-xl p-6 hover-scale">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-purple-100 rounded-lg flex items-center justify-center mr-4">
                            <i class="fas fa-hashtag text-purple-600 text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold">字典（Hash Map）</h3>
                    </div>
                    <p class="text-gray-700 leading-relaxed">
                        是一种基于哈希表的数据结构，用于实现快速的键值对查找。它允许在常数时间内进行插入、删除和查找操作。
                    </p>
                    <div class="mt-4 flex items-center text-sm text-purple-600">
                        <i class="fas fa-check-circle mr-2"></i>
                        <span>O(1) 时间复杂度</span>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 3: 为什么同时使用字典和跳跃表？ -->
        <section class="mb-12 animate-fadeInUp" style="animation-delay: 0.4s">
            <h2 class="text-3xl font-bold mb-6 section-title">
                <i class="fas fa-question-circle mr-3 text-purple-600"></i>为什么同时使用字典和跳跃表？
            </h2>

            <!-- Skip List Advantages -->
            <div class="bg-white rounded-xl card-shadow p-8 mb-6">
                <h3 class="text-2xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-arrow-up mr-2 text-green-500"></i>跳跃表的优势
                </h3>
                
                <div class="space-y-4">
                    <div class="flex items-start">
                        <div class="w-8 h-8 bg-green-100 rounded-full flex items-center justify-center mr-4 mt-1 flex-shrink-0">
                            <i class="fas fa-sort text-green-600 text-sm"></i>
                        </div>
                        <div>
                            <h4 class="font-bold text-lg mb-1">有序性</h4>
                            <p class="text-gray-700">跳跃表维护了元素的有序性，使得可以高效地进行范围查询、按分数排序等操作。它支持对元素进行 <code class="bg-gray-100 px-2 py-1 rounded text-sm">O(log n)</code> 时间复杂度的查找、插入和删除。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start">
                        <div class="w-8 h-8 bg-green-100 rounded-full flex items-center justify-center mr-4 mt-1 flex-shrink-0">
                            <i class="fas fa-compress text-green-600 text-sm"></i>
                        </div>
                        <div>
                            <h4 class="font-bold text-lg mb-1">空间效率</h4>
                            <p class="text-gray-700">跳跃表在空间上相对较为高效，因为它不需要维护额外的数据结构来存储索引，只需维护多层链表。</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Dictionary Advantages -->
            <div class="bg-white rounded-xl card-shadow p-8">
                <h3 class="text-2xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-hashtag mr-2 text-blue-500"></i>字典的优势
                </h3>
                
                <div class="space-y-4">
                    <div class="flex items-start">
                        <div class="w-8 h-8 bg-blue-100 rounded-full flex items-center justify-center mr-4 mt-1 flex-shrink-0">
                            <i class="fas fa-search text-blue-600 text-sm"></i>
                        </div>
                        <div>
                            <h4 class="font-bold text-lg mb-1">快速查找</h4>
                            <p class="text-gray-700">字典通过哈希表提供了 <code class="bg-gray-100 px-2 py-1 rounded text-sm">O(1)</code> 时间复杂度的查找操作，这对于根据元素值进行快速查找非常重要。由于有序集合的元素在跳跃表中是有序的，而字典提供了基于元素值的快速访问。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start">
                        <div class="w-8 h-8 bg-blue-100 rounded-full flex items-center justify-center mr-4 mt-1 flex-shrink-0">
                            <i class="fas fa-map-marker-alt text-blue-600 text-sm"></i>
                        </div>
                        <div>
                            <h4 class="font-bold text-lg mb-1">快速元素定位</h4>
                            <p class="text-gray-700">在Sorted Set中，元素的唯一标识符（如元素的值）需要快速定位。通过字典，可以快速检索到跳跃表中的元素位置。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 4: 综合使用的好处 -->
        <section class="mb-12 animate-fadeInUp" style="animation-delay: 0.5s">
            <h2 class="text-3xl font-bold mb-6 section-title">
                <i class="fas fa-star mr-3 text-purple-600"></i>综合使用的好处
            </h2>
            